<template>
	<view class="form-container">
		<form ref="form" :model="form">
			<view class="form-item">
				<text class="text" prop="payCreditRate">贷记卡费率(%)：</text>
				<input type="digit" class="form-input"  v-model="form.payCreditRate"  :placeholder="payCreditRateValue" @blur="addchange(form.payCreditRate, 'payCreditRate')"/>
			</view>
			<view class="form-item" v-if="channel !== 'liaoyuan'">
				<text class="text">贷记卡提现费(元)：</text>
				<input type="digit" class="form-input" v-model="form.payCreditWithdrawalFee" :placeholder="payCreditWithdrawalFeeValue" />
			</view>
			<view class="form-item">
				<text class="text">汇收银扫码费率：</text>
				<input type="digit" class="form-input" v-model="form.jhPayRate" :placeholder="jhPayRateValue" @blur="addchange(form.jhPayRate, 'jhPayRate')"/>
			</view>
			<view class="form-item">
				<text class="text">汇收银扫码提现费：</text>
				<input type="digit" class="form-input" v-model="form.jhPayWithdrawalFee" :placeholder="jhPayWithdrawalFeeValue" />
			</view>
			<view class="form-item">
				<text class="text">聚收银扫码费率：</text>
				<input type="digit" class="form-input" v-model="form.jhptPayRate" :placeholder="jhptPayRateValue" @blur="addchange(form.jhptPayRate, 'jhptPayRate')"/>
			</view>
			<view class="form-item">
				<text class="text">聚收银扫码提现费：</text>
				<input type="digit" class="form-input" v-model="form.jhptPayWithdrawalFee" :placeholder="jhptPayWithdrawalFeeValue" />
			</view>
			<view class="form-item">
				<text class="text">一码聚扫码费率：</text>
				<input type="digit" class="form-input" v-model="form.zyPayRate" :placeholder="zyPayRateValue" @blur="addchange(form.zyPayRate, 'zyPayRate')"/>
			</view>
			<view class="form-item">
				<text class="text">一码聚扫码提现费：</text>
				<input type="digit" class="form-input" v-model="form.zyPayWithdrawalFee" :placeholder="zyPayWithdrawalFeeValue" />
			</view>
		</form>
		<div class="btn-div">
			<text class="btn-box" @click="sureHandle">确定</text>
		</div>
	</view>
</template>

<script>
	import {
		merchBindTermList,
		changeRateApply,
		changeMerchRateGroupList,
		canChangeRateType,
		getDicts,
		brandList,
		agentList,
		getCtTermById,
		queryFeeRange,
		queryLoginFeeRange
	} from '_a/merchRateChange';
	export default {
		data() {
			return {
				form: {
					serialno: "",
					payCreditRate: "",
					quickCreditRate: "",
					zfbRate: "",
					wxRate: "",
					unionPayRate: "",
					additionalPro: "",
					payCreditWithdrawalFee: "",
					quickCreditWithdrawalFee: "",
					alipayWithdrawalFee: "",
					wxWithdrawalFee: "",
					unionPayWithdrawalFee: "",
					jhPayRate: "",
					jhPayWithdrawalFee: "",
					jhptPayRate: "",
					jhptPayWithdrawalFee: "",
					zyPayRate: "",
					zyPayWithdrawalFee: "",
				},
				payCreditRateValue: "",
				payCreditRateVal: "",
				payCreditWithdrawalFeeValue: "",
				payCreditWithdrawalFeeVal: "",
				jhPayRateValue: "",
				jhPayRateVal: "",
				jhptPayRateValue: "",
				jhptPayRateVal: "",
				jhPayWithdrawalFeeValue: "",
				jhPayWithdrawalFeeVal: "",
				jhptPayWithdrawalFeeValue: "",
				jhptPayWithdrawalFeeVal: "",
				zyPayRateValue: "",
				zyPayRateVal: "",
				zyPayWithdrawalFeeValue: "",
				zyPayWithdrawalFeeVal: "",
				rules: [],
				channel: "",
				// merchData: {},
				termData: {}
			}
		},
		onLoad() {
			this.merchData = this.$storage.getStore("merchDetail") || {};
			this.termData = this.$storage.getStore("termData") || {};
			// console.log('商户信息')
			// console.log(this.merchData)
			// console.log('机具信息')
			// console.log(this.termData)
			this.initForm();
		},
		onUnload() {
			this.$storage.removeStore('merchDetail');
			this.$storage.removeStore('termData');
		},
		methods: {
			async checkEditAgentFeeRange(type){
			 await queryLoginFeeRange(type).then((response) => {
			    if(response.msg =='无操作权限'){
			        this.iscanmodify=false;
			    }else{
			        this.iscanmodify=true;
			    }
			  })
			},
			//初始化表单
			initForm() {
				let that = this
				getCtTermById(that.termData.id).then(res => {
					if (res.code==200 && res.data) {
						that.form.payCreditRate=res.data.payCreditRate;
						that.addchangeV2(that.form.payCreditRate, 'payCreditRate')
						that.form.payCreditWithdrawalFee=res.data.payCreditWithdrawalFee;
						that.form.jhPayRate=res.data.jhPayRate;
						that.addchangeV2(that.form.jhPayRate, 'jhPayRate')
						that.form.jhPayWithdrawalFee=res.data.jhPayWithdrawalFee;
						that.form.jhptPayRate=res.data.jhptPayRate;
						that.addchangeV2(that.form.jhptPayRate, 'jhptPayRate')
						that.form.jhptPayWithdrawalFee=res.data.jhptPayWithdrawalFee;
						that.addchangeV2(that.form.zyPayRate, 'zyPayRate')
						that.form.zyPayWithdrawalFee=res.data.zyPayWithdrawalFee;
					}else{
						that.form.payCreditRate=null;
						that.form.payCreditWithdrawalFee=null;
						that.form.jhPayRate=null;
						that.form.jhPayWithdrawalFee=null;
						that.form.jhptPayRate=null;
						that.form.jhptPayWithdrawalFee=null;
						that.form.zyPayRate=null;
						that.form.zyPayWithdrawalFee=null;
					}
				});
				this.channel = this.merchData.channelCode
				//初始化校验信息
				if(this.channel=="liaoyuan"){
					this.checkRate("merch_rate_liaoyuan");
				}else if(this.channel=="qiantu"){
					this.checkRate("merch_rate_qiantu");
				}else{
					this.checkRate("merch_rate");
				}
			},
			//费率最后一位不写自动补0
			addchange(value, name){
			  if(value && value.length>1){
				let sp= value.split(".");
				if(sp.length>1){
					if(sp[1].toString().length<=1){
					  if(name=="payCreditRate"){
						this.form.payCreditRate = value + "0";
					  }
					  if(name=="jhPayRate"){
						this.form.jhPayRate = value + "0";
					  }
					  if(name=="jhptPayRate"){
						this.form.jhptPayRate = value + "0";
					  }
					  if(name=="zyPayRate"){
						this.form.zyPayRate = value + "0";
					  }
					}
				}
			  }
			},
			//费率最后一位不写自动补0
			addchangeV2(data, name){
			  let value = data.toString()
			  if(value && value.length>1){
				let sp= value.split(".");
				if(sp.length>1){
					if(sp[1].toString().length<=1){
					  if(name=="payCreditRate"){
						this.form.payCreditRate = value + "0";
						// console.log('费率小数payCreditRate' + this.form.payCreditRate)
					  }
					  if(name=="jhPayRate"){
						this.form.jhPayRate = value + "0";
						// console.log('费率小数jhPayRate' + this.form.jhPayRate)
					  }
					  if(name=="jhptPayRate"){
						this.form.jhptPayRate = value + "0";
						// console.log('费率小数jhptPayRate' + this.form.jhptPayRate)
					  }
					  if(name=="zyPayRate"){
						this.form.zyPayRate = value + "0";
					  }
					}
				}
			  }
			},
			async sureHandle() {
				//查询用户是否有修改费率权限
				await this.checkEditAgentFeeRange("switch");
				if(this.iscanmodify==false){
					return this.$tip.toast("用户无修改费率权限，请联系管理员！");
				}
				
				let that = this
				//根据渠道区分需要判断的参数，并判断费率是否在配置范围内，(else里的写法不能换行，不然判断失效)
				if(this.form.payCreditRate==null){
					return this.$tip.modal({content: '请输入贷记卡费率',showCancel: false});
				}else{
					if(!this.payCreditRateVal.test(this.form.payCreditRate))return this.$tip.modal({content: '贷记卡费率只能是'+this.payCreditRateValue,showCancel: false});
				}
				
				if(this.channel =="liaoyuan"){
					
				}
				
				if(this.channel !="liaoyuan"){
					if(this.form.payCreditWithdrawalFee==null){
					return this.$tip.modal({content: '请输入贷记卡提现费',showCancel: false});
					}else{
					 if(!this.payCreditWithdrawalFeeVal.test(this.form.payCreditWithdrawalFee))return this.$tip.modal({content: '贷记卡提现费只能是'+this.payCreditWithdrawalFeeValue,showCancel: false});
					}
				}
				
				//聚合扫码
				if(this.form.jhPayRate==null){
				return this.$tip.modal({content: '请输入汇收银扫码费率',showCancel: false});
				}else{
				 if(!this.jhPayRateVal.test(this.form.jhPayRate))return this.$tip.modal({content: '汇收银扫码费率只能是'+this.jhPayRateValue,showCancel: false});
				}
				if(this.form.jhPayWithdrawalFee==null){
				return this.$tip.modal({content: '请输入汇收银扫码提现费',showCancel: false});
				}else{
				 if(!this.jhPayWithdrawalFeeVal.test(this.form.jhPayWithdrawalFee))return this.$tip.modal({content: '汇收银扫码提现费只能是'+this.jhPayWithdrawalFeeValue,showCancel: false});
				}
				if(this.form.jhptPayRate==null){
				return this.$tip.modal({content: '请输入聚收银扫码费率',showCancel: false});
				}else{
				 if(!this.jhptPayRateVal.test(this.form.jhptPayRate))return this.$tip.modal({content: '聚收银扫码费率只能是'+this.jhptPayRateValue,showCancel: false});
				}
				if(this.form.jhptPayWithdrawalFee==null){
				return this.$tip.modal({content: '请输入聚收银扫码提现费',showCancel: false});
				}else{
				 if(!this.jhptPayWithdrawalFeeVal.test(this.form.jhptPayWithdrawalFee))return this.$tip.modal({content: '聚收银扫码提现费只能是'+this.jhptPayWithdrawalFeeValue,showCancel: false});
				}
				//一码聚
				if(this.form.zyPayRate==null){
				return this.$tip.modal({content: '请输入一码聚扫码费率',showCancel: false});
				}else{
				 if(!this.zyPayRateVal.test(this.form.zyPayRate))return this.$tip.modal({content: '一码聚扫码费率只能是'+this.zyPayRateValue,showCancel: false});
				}
				if(this.form.zyPayWithdrawalFee==null){
				return this.$tip.modal({content: '请输入一码聚扫码提现费',showCancel: false});
				}else{
				 if(!this.zyPayWithdrawalFeeVal.test(this.form.zyPayWithdrawalFee))return this.$tip.modal({content: '一码聚扫码提现费只能是'+this.zyPayWithdrawalFeeValue,showCancel: false});
				}
				
				//校验规则
				let verifyRule = ""
				if(this.channel == "liaoyuan") {
					verifyRule = "merch_rate_liaoyuan"
				}else if(this.channel=="qiantu"){
					verifyRule = "merch_rate_qiantu"
				}else{
					verifyRule = "merch_rate"
				}
				let checkserialnoList = []
				checkserialnoList.push(that.termData.serialno);
				let paramsLists = {
					serialno: checkserialnoList,
					channel: verifyRule,
				 	payCreditRate:this.form.payCreditRate==null?0:this.form.payCreditRate,
				 	payCreditWithdrawalFee:this.form.payCreditWithdrawalFee==null?0:this.form.payCreditWithdrawalFee,
					jhPayRate:this.form.jhPayRate==null?0:this.form.jhPayRate,
					jhPayWithdrawalFee:this.form.jhPayWithdrawalFee==null?0:this.form.jhPayWithdrawalFee,
					jhptPayRate:this.form.jhptPayRate==null?0:this.form.jhptPayRate,
					jhptPayWithdrawalFee:this.form.jhptPayWithdrawalFee==null?0:this.form.jhptPayWithdrawalFee,
					zyPayRate:this.form.zyPayRate==null?0:this.form.zyPayRate,
					zyPayWithdrawalFee:this.form.zyPayWithdrawalFee==null?0:this.form.zyPayWithdrawalFee
				};
				console.log(paramsLists)
				changeRateApply(paramsLists).then(res => {
					this.submitPopup = false;
					if (res.code!=200) {
						this.$tip.modal({
							content: res.msg,
							showCancel: false
						});
					} else {
						this.$tip.modal({
							content: '变更费率申请已受理',
							showCancel: false
						});
						this.init();
					}
				});
			},
			//费率正则表达式（部分表达式有问题）
			selectRateValue(value, type){
			  if(!value || !type){
				return;
			  }
			  if(type==1){
				let arr = value.split("");
				if(parseInt(arr[7])-parseInt(arr[2]) > 1){
				return new RegExp("^"+arr[0]+"\\."+arr[2]+"["+arr[3]+"-9]|["+(parseInt(arr[2])+1)+"-"+(parseInt(arr[7])-1)+"][0-9]|"+arr[7]+"[0-"+arr[8]+"]$");
				}
				if(parseInt(arr[7])-parseInt(arr[2]) == 1){
				  return new RegExp("^"+arr[0]+"\\."+arr[2]+"["+arr[3]+"-9]|"+arr[7]+"[0-"+arr[8]+"]$");
				}
				if(parseInt(arr[7])-parseInt(arr[2]) < 1){
				  return new RegExp("^"+arr[0]+"\\."+arr[2]+"["+arr[3]+"-"+arr[8]+"]$");
				}
			  }
			  if(type==2){
				return new RegExp("^["+value+"]$");
			  }
			},
			//设置费率校验
			checkRate(type){
			  //设置费率校验
			  queryFeeRange(type).then((response) => {
				const datas = response.data;
				console.log("校验数据")
				console.log(datas)
				Object.keys(datas).map((key) => {
				  if (datas[key].dictLabel == ('贷记卡费率')) {
					this.payCreditRateValue= datas[key].dictValue;
					this.payCreditRateVal= this.selectRateValue(this.payCreditRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('云闪付费率')) {
					this.quickCreditRateValue = datas[key].dictValue;
					this.quickCreditRateVal = this.selectRateValue(this.quickCreditRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('支付宝费率')) {
					this.zfbRateValue = datas[key].dictValue;
					this.zfbRateVal = this.selectRateValue(this.zfbRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('微信费率')) {
					this.wxRateValue = datas[key].dictValue;
					this.wxRateVal = this.selectRateValue(this.wxRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('银联扫码费率')) {
					this.unionPayRateValue = datas[key].dictValue;
					this.unionPayRateVal = this.selectRateValue(this.unionPayRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('提现费')) {
					this.additionalProValue = datas[key].dictValue;
					this.additionalProVal = this.selectRateValue(this.additionalProValue, 2);
				  }
				  if (datas[key].dictLabel == ('贷记卡提现费')) {
					this.payCreditWithdrawalFeeValue = datas[key].dictValue;
					this.payCreditWithdrawalFeeVal = this.selectRateValue(this.payCreditWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('云闪付提现费')) {
					this.quickCreditWithdrawalFeeValue = datas[key].dictValue;
					this.quickCreditWithdrawalFeeVal = this.selectRateValue(this.quickCreditWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('支付宝提现费')) {
					this.alipayWithdrawalFeeValue = datas[key].dictValue;
					this.alipayWithdrawalFeeVal = this.selectRateValue(this.alipayWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('微信提现费')) {
					this.wxWithdrawalFeeValue = datas[key].dictValue;
					this.wxWithdrawalFeeVal = this.selectRateValue(this.wxWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('银联扫码提现费')) {
					this.unionPayWithdrawalFeeValue = datas[key].dictValue;
					this.unionPayWithdrawalFeeVal = this.selectRateValue(this.unionPayWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('汇收银扫码费率')) {
					this.jhPayRateValue = datas[key].dictValue;
					this.jhPayRateVal = this.selectRateValue(this.jhPayRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('汇收银扫码提现费')) {
					this.jhPayWithdrawalFeeValue = datas[key].dictValue;
					this.jhPayWithdrawalFeeVal = this.selectRateValue(this.jhPayWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('聚收银扫码费率')) {
					this.jhptPayRateValue = datas[key].dictValue;
					this.jhptPayRateVal = this.selectRateValue(this.jhptPayRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('聚收银扫码提现费')) {
					this.jhptPayWithdrawalFeeValue = datas[key].dictValue;
					this.jhptPayWithdrawalFeeVal = this.selectRateValue(this.jhptPayWithdrawalFeeValue, 2);
				  }
				  if (datas[key].dictLabel == ('一码聚扫码费率')) {
					this.zyPayRateValue = datas[key].dictValue;
					this.zyPayRateVal = this.selectRateValue(this.zyPayRateValue, 1);
				  }
				  if (datas[key].dictLabel == ('一码聚扫码提现费')) {
					this.zyPayWithdrawalFeeValue = datas[key].dictValue;
					this.zyPayWithdrawalFeeVal = this.selectRateValue(this.zyPayWithdrawalFeeValue, 2);
				  }
				})
			  });
			}
		}
	}
</script>

<style scoped lang="scss" >
	.btn-div {
		display: flex;
		justify-content: center;
	}
	.btn-box {
		display: flex;
		justify-content: center;
		align-items: center;
		width: R(320);
		height: R(70);
		line-height: R(50);
		border-radius: R(40);
		color: #fff;
		background: $button-linear-bg;
		font-size: $uni-font-size-lg;
	}
	.form-container {
	  padding: 24rpx 32rpx;
	  background-color: #f8f8f8;
	}
	
	.form-item {
	  position: relative;
	  margin-bottom: 32rpx;
	  padding: 20rpx 24rpx;
	  background-color: #fff;
	  border-radius: 12rpx;
	  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
	  display: flex;
	  align-items: center;
	  transition: all 0.3s ease;
	  
	  &.active {
	    box-shadow: 0 0 0 2rpx #2979ff;
	  }
	}
	
	.form-label {
	  font-size: 28rpx;
	  color: #333;
	  width: 220rpx;
	  flex-shrink: 0;
	}
	
	.form-input {
	  flex: 1;
	  font-size: 28rpx;
	  color: #333;
	  padding: 10rpx 0;
	  border: none;
	  outline: none;
	}
	
	.form-unit {
	  font-size: 28rpx;
	  color: #999;
	  margin-left: 12rpx;
	  width: 40rpx;
	}
	
	.submit-btn {
	  margin-top: 48rpx;
	  background-color: #2979ff;
	  color: #fff;
	  height: 88rpx;
	  border-radius: 44rpx;
	  display: flex;
	  justify-content: center;
	  align-items: center;
	  font-size: 32rpx;
	  box-shadow: 0 4rpx 16rpx rgba(41, 121, 255, 0.3);
	  transition: all 0.3s;
	  
	  &:active {
	    background-color: #1a68e8;
	    transform: translateY(2rpx);
	  }
	}
</style>